<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="card" shadow="never">
        <!-- 头部区域 -->
        <el-form ref="formData" :model="query">
          <el-form-item label="学科名称">
            <el-input class="ipt" v-model="query.subjectName"></el-input>
            <el-button @click="clearBtn">清除</el-button>
            <el-button type="primary" @click="searchBtn">搜索</el-button>
            <el-button
              @click="addSubject"
              class="add"
              type="success"
              icon="el-icon-edit"
              >新增学科</el-button
            >
          </el-form-item>
        </el-form>

        <!-- 数据总数 -->
        <el-alert :title="cTitle" :closable="false" type="info" show-icon>
        </el-alert>

        <!-- 表格区域 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="">
          </el-table-column>

          <el-table-column prop="subjectName" label="学科名称">
          </el-table-column>

          <el-table-column prop="username" label="创建者"> </el-table-column>

          <el-table-column prop="addDate" label="创建日期" width="180px">
            <template v-slot="scope">
              {{ scope.row.addDate | addDateTime }}
            </template>
          </el-table-column>

          <el-table-column prop="isFrontDisplay" label="前台是否显示">
            <template v-slot="scope">
              {{ scope.row.isFrontDisplay === 1 ? "是" : "否"}}
              <!-- {{ scope.row.isFrontDisplay | isFrontDisplay }} -->
            </template>
          </el-table-column>

          <el-table-column prop="twoLevelDirectory" label="二级目录">
          </el-table-column>

          <el-table-column prop="tags" label="标签"> </el-table-column>

          <el-table-column prop="totals" label="题目数量"> </el-table-column>

          <el-table-column label="操作" width="240px">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push(`/subjects/directorys?id=${scope.row.id}&name=${scope.row.subjectName}`)">学科分类</el-button>
              <el-button type="text" size="small" @click="$router.push(`/subjects/tags?id=${scope.row.id}&name=${scope.row.subjectName}`)">学科标签</el-button>
              <el-button type="text" size="small" @click="editBtn(scope.row)">修改</el-button>
              <el-button type="text" size="small" @click="delBtn(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页区域 -->
        <el-pagination
          class="fenye"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="query.pagesize"
          :current-page="query.page"
          background
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </el-card>
    </div>

    <!-- 弹框 -->
    <el-dialog
    @close="cleardlg"
      :title="this.isEdit ? '修改学科' : '新增学科'"
      :visible.sync="showDialog"
      width="400px"
    >
      <el-form
        ref="subjectNameForm"
        :rules="rules"
        :model="form"
        label-width="80px"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input
            autocomplete="off"
            v-model="form.subjectName"
            style="width: 280px"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="form.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list, add, update, remove } from '../../api/hmmm/subjects'
export default {

  name: 'Subjects',
  data () {
    return {
      tableData: [],
      form: {
        subjectName: '',
        isFrontDisplay: true // 是否显示
      },
      query: {
        page: 1, // 页数
        pagesize: 10, // 每页条数
        subjectName: ''
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学习名称', trigger: 'blur' }
        ]
      },

      total: 0, // 数据总数
      showDialog: false, // 是否新增
      isEdit: false, // 是否修改
      editForm: {}
    }
  },

  // 过滤器
  filters: {
    addDateTime (time) {
      return dayjs(time).format('YYYY-MM-DD hh:mm:ss')
    }
  },

  created () {
    // 打开页面渲染数据
    this.subjectList()
  },

  // 数据总条数
  computed: {
    cTitle () {
      return '数据一共' + this.total + '条'
    }
  },

  methods: {
    // 点击关闭清空表单
    cleardlg () {
      this.$refs.subjectNameForm.resetFields()
    },

    // 获取学科列表
    async subjectList () {
      const res = await list(this.query)
      this.tableData = res.data.items
      this.total = res.data.counts
    },

    // 修改学科
    editBtn (row) {
      this.isEdit = true
      this.showDialog = true
      // 数据回显
      this.editForm = row
      this.form.id = this.editForm.id
      this.form.subjectName = this.editForm.subjectName
      if (this.editForm.isFrontDisplay === 1) {
        this.form.isFrontDisplay = true
      } else {
        this.form.isFrontDisplay = false
      }
    },

    // 新增学科
    addSubject () {
      this.isEdit = false
      this.showDialog = true
      this.form.isFrontDisplay = true
    },

    // 点击搜索
    async searchBtn () {
      const res = await list({ subjectName: this.query.subjectName })
      this.tableData = res.data.items
      this.total = res.data.counts
    },

    // 点击清除
    clearBtn () {
      // 清空表单
      this.query.subjectName = ''
      // 新增打开显示
      this.form.isFrontDisplay = true
      this.query = {
        page: 1, // 页数
        pagesize: 10, // 每页条数
        subjectName: ''
      }
      this.subjectList()
    },

    // 当前页
    handleCurrentChange (newPage) {
      this.query.page = newPage
      this.subjectList()
    },

    // 每页条数
    handleSizeChange (newSize) {
      this.query.pagesize = newSize
      this.subjectList()
    },

    // 取消弹框
    closeDialog () {
      this.showDialog = false
    },

    // 点击确定
    confirm () {
      this.$refs.subjectNameForm.validate(async (valid) => {
        if (!valid) return
        if (!this.isEdit) {
          // 添加
          const res = await add(this.form)
          if (res.status === 200) {
            this.$message.success('添加成功!')
          } else {
            this.$message.success('添加失败')
          }
          this.subjectList()

          // 修改
        } else {
          const res = await update(this.form)
          if (res.status === 200 && res.statusText === 'OK') {
            this.$message.success('修改成功')
            this.subjectList()
          } else {
            this.$message.success('修改失败')
          }
        }
        this.showDialog = false
      })
    },

    // 删除按钮
    async delBtn (row) {
      const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message.success('您已取消删除')
      await remove({ id: row.id })
      if (this.tableData.length === 1) {
        this.query.page--
        if (this.query.page <= 1) {
          this.query.page = 1
        }
      }
      this.subjectList()
    }
  }
}

</script>

<style scoped>
.ipt {
  display: inline-block;
  width: 200px;
  margin: 0 13px;
}
.add {
  float: right;
}

.fenye {
  float: right;
  margin-top: 20px;
  margin-bottom: 20px;
}

::v-deep.el-table th {
  background-color: #fafafa;
}
::v-deep.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8
}
</style>
